package com.xyzq.cgate.core.concurrent;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description:
 * @author chenchen
 * @date 2019/6/21 12:56
 */
public class CgateThreadFactory implements ThreadFactory {

    private static final ThreadGroup THREAD_GROUP = new ThreadGroup("cgate");

    private static final AtomicInteger CURRENT_THREAD_NUM = new AtomicInteger(1);


    private boolean daemon;

    private final String namePrefix;

    public CgateThreadFactory(final String namePrefix, final boolean daemon) {
        this.namePrefix = namePrefix;
        this.daemon = daemon;
    }

    public static ThreadFactory create(final String namePrefix, final boolean daemon) {
        return new CgateThreadFactory(namePrefix, daemon);
    }

    @Override
    public Thread newThread(Runnable runnable) {
        Thread thread = new Thread(THREAD_GROUP, runnable,
                THREAD_GROUP.getName() + "-" + CURRENT_THREAD_NUM.getAndIncrement());
        thread.setDaemon(daemon);
        thread.setPriority(Thread.NORM_PRIORITY);
        return thread;
    }
}
